package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;


    @Override
    public PageBean Page(EmpQueryParam empQueryParam) {
        //1. 设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

        //2. 执行查询
        List<Emp> empList = empMapper.list(empQueryParam);

        //3. 解析封装分页结果
        Page<Emp> p = (Page<Emp>) empList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        // 保存员工信息
        empMapper.insert(emp);


        // 保存工作经验
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void EmpDelete(List<Integer> ids) {
        // 批量删除员工
        empMapper.EmpDelete(ids);

        // 批量删除员工对应的工作经验
        empExprMapper.EmpExprDelete(ids);
    }

    @Override
    public Emp EmpById(Integer id) {
//        // 获取员工基本信息
//        Emp emp = empMapper.selectEmpById(id);
//        // 获取员工对应的工作经验
//        List<EmpExpr> exprList = empExprMapper.selectEmpExprById(id);
        Emp emp = empMapper.SelectEmpExprById(id);

        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void EmpUpdate(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        // 修改员工基本信息
        empMapper.EmpUpdate(emp);

        // 修改员工的工作经验
        Integer empId = emp.getId();
        // 删除原来的工作经验
        empExprMapper.EmpExprDelete(Arrays.asList(empId));
//        empExprMapper.EmpExprDeleteById(empId);
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){

            // 添加新的工作经验
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public List<Emp> MasterList() {
        List<Emp> masterList = empMapper.MasterList();
        return masterList;
    }
}
